<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\services\car;

use app\dao\car\CarServiceDao;
use app\services\BaseServices;
use crmeb\exceptions\AdminException;
use crmeb\services\FormBuilder as Form;
use think\facade\Route as Url;

class CarServiceServices extends BaseServices
{
    public function __construct(CarServiceDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 列表
     * @param array $where
     * @param bool $is_page
     * @return array
     */
    public function getList(array $where = [],bool $is_page = false){
        $page = $limit = 0;
        if($is_page) [$page,$limit] = $this->getPageValue();
        $list = $this->dao->getList($where,$page,$limit,$is_page);
        foreach ($list['list'] as &$item) {
            $item['add_time'] = date('Y-m-d H:i:s',$item['add_time']);
        }
        return $list;
    }

    /**
     * 添加|修改
     * @param int $id
     * @return array
     * @throws \FormBuilder\Exception\FormBuilderException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add(int $id)
    {
        $info = $this->dao->get($id);
        $title = $id ? '修改' : '添加';
        $field = array();
        $field[] = Form::hidden('id',$info['id']??0);
        $field[] = Form::input('name','服务名称',$info['name']??'');
        $field[] = Form::textarea('remark', '服务简介', $info['remark']??'')->rows(2)->autoSize(2,4);
        $field[] = Form::number('price', '价格', $info['price']??'')->min(0)->style(['width'=>'100%']);
        $field[] = Form::radio('status','是否显示',$info['status']??1)->options([['value'=>1,'label'=>'显示'],['value'=>0,'label'=>'隐藏']]);
        return create_form($title, $field, Url::buildUrl('/car/service/save'), 'POST');
    }

    /**
     * 保存
     * @param array $data
     * @return string
     */
    public function save(array $data)
    {
        if(empty($data['id'])) {
            unset($data['id']);
            $data['add_time'] = time();
            if (!$this->dao->save($data)) throw new AdminException('添加失败');
        } else {
            if(!$this->dao->update($data['id'],$data)) throw new AdminException('修改失败');
        }
        return "提交成功";
    }

    /**
     * 删除
     * @param int $id
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete (int $id) {
        if(!$this->dao->get($id)) throw new AdminException('未查询到数据');
        if(!$this->dao->update($id,['is_del'=>1,'delete_time'=>time()])) throw new AdminException('删除失败');
        return '删除成功';
    }

    /**
     * 显示|隐藏
     * @param int $id
     * @param int $status
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function setShow(int $id,int $status) {
        if(!$this->dao->get($id)) throw new AdminException('未查询到数据');
        if(!$this->dao->update($id,['status'=>$status])) throw new AdminException($status == 1 ? '显示失败' : '隐藏失败');
        return $status == 1 ? '显示成功' : '隐藏成功';
    }
}
